import os
import django

# 初始化django配置，注意修改blog_manager为项目名
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blog_manager.settings")
django.setup()

# 以下代码导入模型，根据情况修改
from post.models import *

from blog_manager import settings
BASE_DIR = settings.BASE_DIR
# 初始化数据
# os.system(f'python {BASE_DIR}\manage.py loaddata topic.json')
# os.system(f'python {BASE_DIR}\manage.py loaddata comment.json')
# os.system(f'python {BASE_DIR}\manage.py loaddata user.json')
# os.system(f'python {BASE_DIR}\manage.py loaddata role.json')

# TODO 原生查询

# raw_qs = Topic.objects.raw("select  * from post_topic order by created_time desc")
# for t in raw_qs:
#     print(t.title, t.created_time)

# raw()方法存在 SQL注入 问题
# name = '李四(管理员)'
name = "' or '1'='1"

# 字符串拼接SQL
# rqs = User.objects.raw("select id, username, password from auth_user where username='" + name + "'")

# 参数化SQL
rqs = User.objects.raw("select id, username from auth_user where username=%s", [name])

for t in rqs:
    print(t.id, t.username, t.password)
